package com.dingding.order.core.march.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.order.code.CalculateDistanceCodeMsg;
import com.dingding.order.core.march.biz.IMarchOrderService;
import com.dingding.order.core.march.dao.IMarchUserOrderTDao;
import com.dingding.order.dto.march.ReqMarchOrderDTO;
import com.dingding.order.dto.march.ResMarchOrderDTO;
import com.dingding.order.dto.march.ResMarchOrderList;
import com.dingding.order.dto.march.ResTaxiMarchOrder;

/**
 * @ClassName:MarchOrderServiceImpl
 * @Description:进行中订单实现
 * @author:chenwei
 * @date: 2016年6月2日 下午9:24:49
 */
@SuppressWarnings("all")
@Component("marchOrderService")
public class MarchOrderServiceImpl extends AbstractResponse<ResMarchOrderList, ReqMarchOrderDTO>
		implements IMarchOrderService {

	@Resource(name = "marchUserOrderTDao")
	private IMarchUserOrderTDao marchUserOrderTDao;

	/**
	 * 
	 * 获取进行中订单
	 * 
	 * @Title: marchOrder
	 * @author chenwei
	 * 
	 *         修改说明： 由于当前代码重构是在新版本发布之前，故不存在兼容性问题，所以重构之前的代码不再保存 修改人：CHEHUANBO
	 *         修改原因：代码逻辑存在问题，重构代码 修改时间：2016-06-12 修改版本：V2.2.0
	 * 
	 *         修改说明：添加出租车业务 修改人：zhouxuan 修改原因:增加出租车业务 修改时间：2016/7/4 修改版本:V2.3.0
	 * 
	 */
	@Override
	public String getMarchOrder(String requestDate) {

		ResponseEntity<ResMarchOrderList> response = new ResponseEntity<ResMarchOrderList>();
		ReqMarchOrderDTO reqMarchOrder = super.parseRequestJson(requestDate);
		// 1 .验证数据
		if (!validateParam(response, reqMarchOrder)) {
			return super.responseBeanToJson(response);
		}

		// 2. 获取所有私家车未完成的订单
		List<ResMarchOrderDTO> resMarchOrderDTOs = marchUserOrderTDao.getMarchOrderDaoList(reqMarchOrder);

		// 3. 获取所有出租车未完成订单
		List<ResTaxiMarchOrder> resTaxiMarchOrders = marchUserOrderTDao.getTaxiMarchOrderDaoList(reqMarchOrder);

		// 4. 将私家车订单分类为长途和短途
		ResMarchOrderList resMarchOrdersList = splitOrder(resMarchOrderDTOs, resTaxiMarchOrders);

		response.setResponseBody(resMarchOrdersList);
		response.setCode(HttpStatus.SC_OK);

		return super.responseBeanToJson(response);

	}

	/**
	 * 
	 * 将所属用户进行中订单，拆分为短途订单和长途订单
	 * 
	 * 说明：拆分的目的是为了方便客户端进行订单缓存
	 * 
	 * @Title: splitOrder
	 * @Description: 拆分订单
	 * @author chenwei 修改说明： 由于当前代码重构是在新版本发布之前，故不存在兼容性问题，所以重构之前的代码不再保存
	 *         修改人：CHEHUANBO 修改原因：重构代码 修改时间：2016-06-12 修改版本：V2.2.0
	 * 
	 *         修改人：zhouxuan 修改原因：添加出租车业务 修改时间：2016-07-4 修改版本：V2.3.0
	 */
	private ResMarchOrderList splitOrder(List<ResMarchOrderDTO> marchOrder,
			List<ResTaxiMarchOrder> resTaxiMarchOrders) {

		ResMarchOrderList marchOrderList = new ResMarchOrderList();

		// 短途订单集合
		List<ResMarchOrderDTO> shortMarchOrder = new ArrayList<ResMarchOrderDTO>();
		// 长途订单集合
		List<ResMarchOrderDTO> longMarchOrder = new ArrayList<ResMarchOrderDTO>();

		// 存在进行中订单，进行拆分
		if (marchOrder != null && marchOrder.size() > 0) {

			for (ResMarchOrderDTO resMarchOrder : marchOrder) {
				if (resMarchOrder.getIsLongDistance() == 1) { // 普通订单
					shortMarchOrder.add(resMarchOrder);
				} else if (resMarchOrder.getIsLongDistance() == 2) { // 长途订单
					longMarchOrder.add(resMarchOrder);
				}
			}
		}

		/**
		 * 如果存在两个或两个以上短途单，则移除待抢单订单
		 */
		if (shortMarchOrder.size() > 1) {
			// 遍历短途单列表
			for (int i = 0; i < shortMarchOrder.size(); i++) {
				// 如果当前订单为待抢单订单，则移除
				if (shortMarchOrder.get(i).getOrderStatus() == 1) {
					shortMarchOrder.remove(i);
				}
			}
		}

		/**
		 * 如果存在两个或两个以上长途单，则移除待抢单订单
		 */
		if (longMarchOrder.size() > 1) {
			// 遍历短途单列表
			for (int i = 0; i < longMarchOrder.size(); i++) {
				// 如果当前订单为待抢单订单，则移除
				if (longMarchOrder.get(i).getOrderStatus() == 1) {
					longMarchOrder.remove(i);
				}
			}
		}

		marchOrderList.setShortMarchOrderList(shortMarchOrder);
		marchOrderList.setLongMarchOrderList(longMarchOrder);
		marchOrderList.setTaxiOrderList(resTaxiMarchOrders);

		return marchOrderList;

	}

	// /**
	// * @Title: assemblyContent
	// * @Description: 封装换回数据
	// * @param @param response 返回数据
	// * @param @param marchOrderList 传入参数
	// * @author chenwei
	// * @return void
	// * @throws
	// */
	// private void assemblyContent(ResponseEntity<ResMarchOrderList> response,
	// ResMarchOrderList resMarchOrdersList,
	// int responseType) {
	//
	// if (responseType == 0) {
	// response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
	// } else if (responseType == 1) {
	// response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
	// } else {
	// response.setCode(HttpStatus.SC_OK);
	// }
	// response.setResponseBody(resMarchOrdersList);
	// }
}
